package org.linlinjava.litemall.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期格式化工具类
 */
public class DateTimeUtil {

    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(dateTime);

        return strDate2;
    }

    public static long compareTime(String beforeDate, String afterDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date dateBefore = null;
        java.util.Date dateAfter = null;
        try {
            dateBefore = sdf.parse(beforeDate);
            dateAfter = sdf.parse(afterDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long rst = dateBefore.getTime() - dateAfter.getTime();
        return rst;
    }

    public static String getFautureTimeByTime(String dateTime, long timeLen) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long fautureTime = date.getTime() + timeLen;
        Date fautureDate = new Date(fautureTime);
        String rst = dateFormat.format(fautureDate);
        return rst;
    }


}
